package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.management.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: chouh
 * @DateTime: 2025/10/8 19:56
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/roles")
@SuppressWarnings("all")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/list")
    public Result queryAllRoles(){
        log.info("queryAllRoles");
        List<Role> result =roleService.queryAllRoles();
        return Result.success(result);
    }

    @PutMapping
    public Result updateRole(@RequestBody Role role) {
        log.info("updateRole:{}",role);
        roleService.updateRole(role);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result queryRoleById(@PathVariable Integer id) {
        log.info("queryRoleById:{}",id);
        Role role = roleService.queryRoleById(id);
        return Result.success(role);
    }

    @PostMapping
    public Result addRole(@RequestBody Role role){
        log.info("insertRole:{}",role);
        roleService.addRole(role);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteRole(@PathVariable Integer id) {
        log.info("deleteRole:{}",id);
        roleService.deleteRole(id);
        return Result.success();
    }

    @GetMapping
    public Result page(@RequestParam(name = "name", required = false) String name,
                       @RequestParam(name = "label", required = false) String label,
                       @RequestParam(name = "page", defaultValue = "1") Integer page,
                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        log.info("page:{},pageSize:{},name:{},label:{}", page, pageSize, name, label);
        PageResult<Role> pageResult = roleService.page(name, label, page, pageSize);
        return Result.success(pageResult);
    }
}
